package 力扣.链表;

public class 移除链表元素203 {
    // 只要有了假头 不管后面的节点怎么重新赋值 都不会改变整个链表 但是使用'last.next = head'会改变整个链表
    public ListNode removeElements(ListNode head, int val) {

        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode last = dummy;
        while (head != null) {
            if (head.val == val) {
                head = head.next;
                last.next = head;
            }else {
                last = head;
                head = head.next;
            }
        }
        return dummy.next;
    }
}
